package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/shuzhi")
public class WordController {

    @GetMapping(value = "/generate-word", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<Resource> invokePython() throws IOException {
        String pythonScriptPath = "E:\\pycharm\\number_reasoning\\QDGAT-master-chinese\\data\\dorp\\script.py";
        String tempFilePath = "E:\\table.docx";
        String anacondaPythonPath = "C:\\anaconda3\\python.exe";
        String wordContent = "Hello, World!"; // Word文件的内容
        ProcessBuilder processBuilder = new ProcessBuilder(anacondaPythonPath, pythonScriptPath,wordContent, tempFilePath);

        // 启动Python进程
        Process process = processBuilder.start();

        // 等待Python进程执行完成
        try {
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                // 读取生成的Word文件
                File file = new File(tempFilePath);
                Resource resource = new FileSystemResource(file);

                // 设置HTTP响应头
                HttpHeaders headers = new HttpHeaders();
                headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());

                return ResponseEntity.ok()
                        .headers(headers)
                        .contentLength(file.length())
                        .contentType(MediaType.APPLICATION_OCTET_STREAM)
                        .body(resource);
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
    @GetMapping("/de")
  public  String de(String tempFilePath){
        tempFilePath="E:\\"+tempFilePath;
                File tempFile = new File(tempFilePath); // 修改为实际的临时文件路径
                if (tempFile.exists()) {
                    tempFile.delete();
                    return "成功";
                }
                return  "成功";
    }

    @PostMapping("/getshuzhi")
    public AjaxResult  importData()
    {
        Map<String,Object> result=new HashMap<>();
        result.put("number",580680);
        return  AjaxResult.success(result);
//        if(resultList!=null&&resultList.size()!=0){
//            util.exportExcel(response, resultList, "问题数据");
//        }
    }
    @Anonymous
    //开学时输入身份证查询信息
    @GetMapping("/test")
    public String test(String test)
    {
        return "you send :"+test;
    }
}
